/*
 *@Type SocketClient.java
 * @Desc
 * @Author urmsone urmsone@163.com
 * @date 2024/6/13 13:15
 * @version
 */
package client;

import dto.ActionDTO;
import dto.ActionTypeEnum;
import dto.RespDTO;
import service.Node;
import utils.Role;

import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Map;

public class SocketClient implements Client {
    private String host;
    private int port;


    public SocketClient(List<Node> clusterNodes) {
        // 从集群节点中找到主节点
        Node masterNode = clusterNodes.stream()
                .filter(node -> node.getRole() == Role.MASTER)
                .findFirst()
                .orElseThrow(() -> new RuntimeException("No master node found"));

        String[] addr = masterNode.getAddress().split(":");
        this.host = addr[0];
        this.port = Integer.parseInt(addr[1]);
    }

    @Override
    public void set(String table, String key, Object value) {
        try (Socket socket = new Socket(host, port);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
            // 传输序列化对象
            ActionDTO dto = new ActionDTO(ActionTypeEnum.SET, table, key, value);
            oos.writeObject(dto);
            oos.flush();
            RespDTO resp = (RespDTO) ois.readObject();
            System.out.println("resp data: "+ resp.toString());
            // 接收响应数据

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //批量增改
    public void mSet(String table, Map<String, Object> entries) {
        for (Map.Entry<String, Object> entry : entries.entrySet()) {
            set(table, entry.getKey(), entry.getValue());
        }
    }


    @Override
    public Object get(String table, String key) {
        RespDTO resp = null;
        try (Socket socket = new Socket(host, port);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
            // 传输序列化对象
            ActionDTO dto = new ActionDTO(ActionTypeEnum.GET, table, key, null);
            oos.writeObject(dto);
            oos.flush();
            resp = (RespDTO) ois.readObject();
            System.out.println("resp data: "+ resp.toString());
            // 接收响应数据

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return resp.getValue();

    }

    @Override
    public void rm(String table, String key) {
        try (Socket socket = new Socket(host, port);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
            // 传输序列化对象
            ActionDTO dto = new ActionDTO(ActionTypeEnum.RM, table, key, null);
            oos.writeObject(dto);
            oos.flush();
            RespDTO resp = (RespDTO) ois.readObject();
            System.out.println("resp data: "+ resp.toString());
            // 接收响应数据
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //批量删除
    public void mRemove(String table, List<String> keys) {
        for (String key : keys) {
            rm(table,key);
        }
    }

}
